$(function () {
    var totalPage = $('span#totalPage').text();
    var userName = $('#exampleInputName2').val();
    var a = parseInt(totalPage);
    if (a > 0) {
        $('#pagination-demo').twbsPagination({
            totalPages: a,
            visiblePages: 5,
            first: '首页',
            prev: "<<",
            next: '>>',
            last: '最后一页',
            onPageClick: function (event, page) {
                event.preventDefault();
                $.ajax({
                    type: 'post',
                    url: '/administrator/viewlist',
                    data: {
                        pageIndex: page,
                        condition: userName
                    },
                    dataType:'json',
                    success: function (data) {
                        if (data.status==1) {
                            var str = "";
                            var admin = data.data;
                            console.log(admin);
                            for (var i = 0; i < admin.list.length; i++) {
                                str += "<tr>\n" +
                                    " \t<td><input type='checkbox' name='ids' value='" + admin.list[i].id + "'></td>\n" +
                                    " <td>" + (i + 1) + "</td>\n" +
                                    " <td>" + admin.list[i].login_code + "</td>\n" +
                                    " <td style =''>" + admin.list[i].phone + "</td>\n" +
                                    " <td style =''>" + admin.list[i].email + "</td>\n" +
                                    " <td style =''>" + (admin.list[i].role_id==1?'超级管理员':'普通管理员') + "</td>\n" +
                                    " <td style =''>" + (new Date((admin.list[i].join_time)).Format('yyyy-MM-dd hh:mm:ss'))+ "</td>\n" +
                                    " <td class='td-status'><span class=\"layui-btn layui-btn-normal \" onclick=\"admin_stop(this,'"+admin.list[i].id+"','"+admin.list[i].status+"')\">" + (admin.list[i].status == 1 ? '启用' : '未启用') + "</td>\n" +
                                    " <td style ='display: none'>" + admin.list[i].id + "</td>\n" +
                                    " <td class=\"td-manage\">" +
                                    "                <a title=\"编辑\" href=\"javascript:;\" onclick=\"admin_edit('编辑','/administrator/edit.html','"+admin.list[i].id+"','','510')\"\n" +
                                    "                   class=\"ml-5\" style=\"text-decoration:none\">\n" +
                                    "                    <i class=\"layui-icon\">&#xe642;</i>\n" +
                                    "                </a>\n" +
                                    "                <a title=\"删除\" href=\"javascript:;\" onclick=\"admin_del(this,'"+admin.list[i].id+"')\"\n" +
                                    "                   style=\"text-decoration:none\">\n" +
                                    "                    <i class=\"layui-icon\">&#xe640;</i>\n" +
                                    "                </a>" +
                                    "</td>\n" +
                                    "</tr>";
                            }
                            var trNode = $('#ulContainer');
                            trNode.html(str);

                        } else {
                            swal(data.msg, "", "error");
                        }
                    },
                    error: function () {
                        swal('出现错误', "", "error");
                    }
                });
            }
        });
    }

});

$(function () {
//全选全部取消-步骤（当选中前面的按钮、下面所有都被选中 反之，下面所有都被取消选中）
    $("#all").click(function(){
        if($(this).is(":checked")){ //如果被选中
            $("[name=ids]").prop("checked",true); //添加选中状态
        }else{
            $("[name=ids]").prop("checked",false);//取消选中状态
        }
    });

    $(document).on('click', '.userNameQuery', function (e) {
        var userName = $('#exampleInputName2').val();
        $.ajax({
            type: 'post',
            url: '/administrator/viewlist',
            data: {
                condition: userName
            },
            dataType:'json',
            success: function (data) {
                if (data.status==1) {
                    var str = "";
                    var admin = data.data;
                    console.log(admin);
                    for (var i = 0; i < admin.list.length; i++) {
                        str += "<tr>\n" +
                            " \t<td><input type='checkbox' name='ids' value='" + admin.list[i].id + "'></td>\n" +
                            " <td>" + admin.list[i].id + "</td>\n" +
                            " <td>" + admin.list[i].login_code + "</td>\n" +
                            " <td style =''>" + admin.list[i].phone + "</td>\n" +
                            " <td style =''>" + admin.list[i].email + "</td>\n" +
                            " <td style =''>" + (admin.list[i].role_id==1?'超级管理员':'普通管理员') + "</td>\n" +
                            " <td style =''>" + (new Date((admin.list[i].join_time)).Format('yyyy-MM-dd hh:mm:ss'))+ "</td>\n" +
                            " <td class='td-status'><span class=\"layui-btn layui-btn-normal \" onclick=\"admin_stop(this,'1',0)\">" + (admin.list[i].status == 1 ? '启用' : '未启用') + "</td>\n" +
                            " <td style ='display: none'>" + admin.list[i].id + "</td>\n" +
                            " <td class=\"td-manage\">" +
                            "                <a title=\"编辑\" href=\"javascript:;\" onclick=\"admin_edit('编辑','/administrator/adminedit.html','"+$(this).parent('td').prev().text()+"','','510')\"\n" +
                            "                   class=\"ml-5\" style=\"text-decoration:none\">\n" +
                            "                    <i class=\"layui-icon\">&#xe642;</i>\n" +
                            "                </a>\n" +
                            "                <a title=\"删除\" href=\"javascript:;\" onclick=\"admin_del(this,'1')\"\n" +
                            "                   style=\"text-decoration:none\">\n" +
                            "                    <i class=\"layui-icon\">&#xe640;</i>\n" +
                            "                </a>" +
                            "</td>\n" +
                            "</tr>";
                    }
                    var trNode = $('#ulContainer');
                    trNode.html(str);
                   e.preventDefault();
                } else {
                    swal(data.msg, "", "error");
                }
            },
            error: function () {
                swal('出现错误', "", "error");
            }
        });

    });


    //数据回填
    var id = null;
    $(document).on('click', ".updateModel", function () {
        id = $(this).parent('td').prev().text();
        $.ajax({
            type: 'get',
            url: '/administrator/getById/' + id,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    var au = data.data;
                    $('#u_logincode').val(au.loginCode);
                    $('#u_username').val(au.userName);
                    $('#u_role_id option').each(function () {
                        if ($(this).val() == au.role_id) {
                            $(this).attr("selected", true);
                        }
                    });
                    $('#u_login_code').val(au.login_code);
                    $('#u_name').val(au.name);
                    $('#u_admin').val(au.email);
                    $('#u_phone').val(au.phone);
                    $('#u_email').val(au.email);
                    $('#u_password').val(au.password);
                    $('#u_pimages').attr('src',au.admin_pic);
                } else {
                    swal(data.msg, "", "error");
                }
            },
            error: function () {
                swal("出现错误", "", "error");
            }
        })
    });

});
//日期格式转换
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}